﻿using Exam.infranstuctrue.Dto.Request.Exercise;
using Exam.infranstuctrue.Tools;
using Exam.Service.Interface;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Exam.Controllers
{
    [ApiController]
    [Authorize]
    public class ExerciseController : BaseController
    {
        private readonly IExerciseServer _exerciseServer;
        private readonly Session _session;

        public ExerciseController(IExerciseServer exerciseServer, Session session)
        {
            _exerciseServer = exerciseServer;
            _session = session;
        }
        /// <summary>
        /// 开始刷题
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetQuestionBank()
        {
            var uid = _session.Uid;
            var result = await _exerciseServer.GetQuestionBank(uid);
            return Ok(result);
        }
        /// <summary>
        /// 结束刷题
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> EndExercisServer(ExerciseDto dto)
        {
            dto.EUid = _session.Uid;
            var result = await _exerciseServer.EndExercisServer(dto);
            return Ok(result);
        }
        /// <summary>
        /// 获取任务
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetTask()
        {
            var uid = _session.Uid;
            var result = await _exerciseServer.GetTask(uid);
            return Ok(result);
        }
        /// <summary>
        /// 刷新完成任务
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> RefreshTask(ExerciseDto dto)
        {
            dto.EUid = _session.Uid;
            var result = await _exerciseServer.RefreshTask(dto);
            return Ok(result);
        }
        /// <summary>
        /// 获取试卷
        /// </summary>
        /// <param name="lable"></param>
        /// <param name="lv"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetTestpaper(string? label, int? lv)
        {
            var result = await _exerciseServer.GetTestpaper(label, lv);
            return Ok(result);
        }
        /// <summary>
        /// 开始模拟
        /// </summary>
        /// <param name="tpid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetBeginSimulationTask(string? tpid)
        {
            var uid = _session.Uid;
            var result = await _exerciseServer.BeginSimulation(uid, tpid);
            return Ok(result);
        }
        /// <summary>
        /// 获取试题
        /// </summary>
        /// <param name="tpid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetQuestionSimulation(string? tpid)
        {
            var result = await _exerciseServer.GetQuestionSimulation(tpid);
            return Ok(result);
        }
        /// <summary>
        /// 结束模拟
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> EndSimulation(SimulationDto dto)
        {
            dto.uid = _session.Uid;
            var result = await _exerciseServer.EndSimulation(dto);
            return Ok(result);
        }
        /// <summary>
        /// 获取以模拟试卷
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetSimulationTestPaper()
        {
            var uid = _session.Uid;
            var result = await _exerciseServer.GetSimulationTestPaper(uid);
            return Ok(result);
        }
        /// <summary>
        /// 模拟详情
        /// </summary>
        /// <param name="tpid"></param>
        /// <param name="sid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> SimulationParticulars(string? tpid, string sid)
        {
            var uid = _session.Uid;
            var result = await _exerciseServer.SimulationParticulars(uid, tpid, sid);
            return Ok(result);
        }
        /// <summary>
        /// 刷题数量排名
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> ExerciseCountRanking()
        {
            var uid = _session.Uid;
            var token = _session.GetToken();
            var result = await _exerciseServer.ExerciseCountRanking(uid,token);
            return Ok(result);
        }
        /// <summary>
        /// 刷题正确率排名
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> ExerciseAccuracyRanking()
        {
            var uid = _session.Uid;
            var token = _session.GetToken();
            var result = await _exerciseServer.ExerciseAccuracyRanking(uid,token);
            return Ok(result);
        }
        /// <summary>
        /// 刷新旧任务
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> RefreshOldTask()
        {
            var result = await _exerciseServer.RefreshOldTask();
            return Ok(result);
        }
    }
}
